import mongoose from 'mongoose'
import autoInc from 'mongoose-auto-increment'
mongoose.connect('mongodb://localhost/das_v_development')
const db = mongoose.connection
autoInc.initialize(db)
//数据库字段转换码
const trans = {
    unitID: 'a',
    unitName: 'b',
    serviceID: 'c',
    productTypeID: 'd',
    version: 'd0',
    manufacturers: 'd1',
    deviceID: 'e',
    userGuid: 'f',
    country: 'g',
    prove: 'h',
    city: 'i',
    village: 'j',
    address: 'k',
    resourceKind: 'l',
    resourceType: 'm',
    resourceGuid: 'n',
    resourceFatherGuid: 'n0',
    resourceName: 'o',
    issue: 'p',
    launch: 'q',
    clientType: 'r',
    publish: 'u',
    author: 'v',
    forum: 'w',
    categoryName: 'x',
    pageID: 'y',
    visitedDate: 'z',
    _visitedDate: 'z0',
    retentionTime: 'a0',
    userAction: 'a1',
    dateType: 'a2',
    event: 'a3',
    createdAt: 'a4',
    tag: 'a5',
    deviceType: 'a6',
    ip: 'a7',
    dataSource: 'a8',
    expired: 'a9',
    index: 'b0'
}
const analytics = {
        [trans.unitID]: {
            type: String,
            index: true,
            trim: true
        },                                      //单位编号
        [trans.unitName]: {
            type: String,
            index: true,
            trim: true
        },                                      //单位名称
        [trans.serviceID]: {
            type: String,
            index: true,
            trim: true
        },                                      //服务编号
        [trans.productTypeID]: {
            type: String,
            index: true
        },                                      //产品类型 机器标识
        [trans.version]: {
            type: String,
            index: true
        },                                      //软件版本号
        [trans.manufacturers]: {
            type: String,
            index: true
        },                                      //硬件厂商
        [trans.deviceID]: {
            type: String,
            index: true,
            trim: true
        },                                      //设备编号
        [trans.clientType]: {
            type: Number,
            index: true
        },                                      //客户端类型 安卓，ios
        [trans.userGuid]: {
            type: String,
            index: true,
            trim: true
        },                                      //用户编号
        [trans.deviceArea]: {
            type: String,
            index: true,
            trim: true
        },                                      //设备地域
        [trans.country]: {
            type: String,
            index: true,
            trim: true
        },                                      //国家
        [trans.prove]: {
            type: String,
            index: true
        },                                      //省
        [trans.city]: {
            type: String,
            index: true
        },                                      // 市
        [trans.village]: {
            type: String,
            index: true
        },                                      //县
        [trans.address]: {
            type: String,
            index: true
        },                                      //详细地址
        [trans.resourceKind]: {
            type: Number,
            index: true
        },                                      //资源种类 如：杂志，图书
        [trans.resourceType]: {
            type: Number,
            index: true
        },                                      //资源类型 如：文本， 原貌
        [trans.resourceGuid]: {
            type: String,
            index: true,
            trim: true
        },                                      //资源编号
        [trans.resourceFatherGuid]: {
            type: String,
            index: true,
            trim: true
        },                                      //资源父级编号
        [trans.resourceName]: {
            type: String,
            index: true,
            trim: true
        },                                      //资源名称
        [trans.issue]: {
            type: Number,
            index: true
        },                                      //刊期
        [trans.launch]: {
            type: Number,
            index: true
        },                                      //发行时间
        [trans.publish]: {
            type: Number,
            index: true
        },                                      //出版社
        [trans.author]: {
            type: String,
            trim: true
        },                                       //作者
        [trans.forum]: {
            type: String,
            index: true
        },                                       //模块名称
        [trans.categoryName]: {
            type: String,
            index: true
        },                                       //资源分类
        [trans.pageID]: {
            type: String,
            trim: true
        },                                       //页面标识
        [trans.visitedDate]: {
            type: Date,
            index: true
        },                                       //访问时间
        [trans._visitedDate]: {
            type: Number,
            index: true
        },                                       //访问时间搓
        [trans.retentionTime]: {
            type: Number,
            index: true
        },                                       //阅读时长
        [trans.userAction]: {
            type: Number,
            index: true
        },                                       //用户行为
        [trans.dateType]: {
            type: Number
        },                                      //数据类型
        [trans.event]: {
            type: Number
        },                                      //事件模版
        [trans.createdAt]: {
            type: Date,
            default: Date.now
        },                                      //入库时间
        [trans.tag]: {
            type: String
        },                                       //标签 目前意思：通道
        [trans.deviceType]: {
            type: Number
        },                                       //设备类型，dss特有1,竖屏，2为宽屏
        [trans.ip]: {
            type: String
        },                                      //访问ip
        [trans.dataSource]: {
            type: Number,
            index: true
        },                                       //数据来源固定值，便于查询
        [trans.expired]: {
            type: String
        },                                       //过期时间
        [trans.index]: {
            type: String
        }                                        //索引值
    }

const schema = new mongoose.Schema(analytics)
schema.plugin(autoInc.plugin, {
        model: 'analytics',
        field: '_snid',
        startAt: 1
})
const model = mongoose.model('analytics', schema)

const new_schema = new mongoose.Schema(analytics)
new_schema.plugin(autoInc.plugin, {
        model: 'newanalytics',
        field: '_snid',
        startAt: 1
})
const new_model = mongoose.model('newanalytics', schema)

const new_create = function (data) {
    return new Promise((resolve, reject) => {
        console.log(JSON.stringify(data))
        new_model.create(data, (err, res) => {
            if (err) {
                return reject(err) //reject 返回错误
            }
            return resolve(res) //resolve 返回结果
        })
    })
}

const create = function (data) {
    return new Promise((resolve, reject) => {
        console.log(JSON.stringify(data))
        model.create(data, (err, res) => {
            if (err) {
                return reject(err) //reject 返回错误
            }
            return resolve(res) //resolve 返回结果
        })
    })
}

const find = function (query) {
    return new Promise((resolve, reject) => {
        model.find(query, null, (err, res) => {
            if (err) {
                return reject(err) //reject 返回错误
            }
            return resolve(res) //resolve 返回结果
        })
    })
}

const new_find = function (query) {
    return new Promise((resolve, reject) => {
        new_model.find(query, null, (err, res) => {
            if (err) {
                return reject(err) //reject 返回错误
            }
            return resolve(res) //resolve 返回结果
        })
    })
}

const new_delete = function (query){
    return new Promise((resolve, reject) => {
        new_model.remove(query, (err, res) => {
            if (err) {
                return reject(err) //reject 返回错误
            }
            return resolve(res) //resolve 返回结果
        })
    })
}

export default {
    create,
    new_create,
    find,
    new_find,
    new_delete
}